﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace EpubFactory.Common
{
    public class CoverOption
    {
        public Size Size { get; set; }
        public class Item
        {
            [XmlElement("BgColor")]
            public int BgColorInt
            {
                get { return BgColor.ToArgb(); }
                set { BgColor = Color.FromArgb(value); }
            }
            [XmlElement("ForeColor")]
            public int ForeColorInt
            {
                get { return ForeColor.ToArgb(); }
                set { ForeColor = Color.FromArgb(value); }
            }
            [XmlIgnore]
            public Color BgColor { get; set; }
            [XmlIgnore]
            public Color ForeColor { get; set; }
            public SimpleFont SimpleFont { get; set; }
        }
        public class SimpleFont
        {
            public string Family { get; set; }
            public int Size { get; set; }

            public Font ToFont()
            {
                return new Font(this.Family, this.Size);
            }
        }

        public Item Title { get; set; }
        public Item Author { get; set; }
    }

    public class ImageUtil
    {
        public static Image Copy(string path)
        {
            if (File.Exists(path))
            {
                using (Image im = Image.FromFile(path))
                {
                    Bitmap bm = new Bitmap(im);
                    return bm;
                }
            }
            return null;
        }
        public static Image Generate(string title, string author, CoverOption option)
        {
            if (option == null)
            {
                option = AppPreference.DefaultCoverOption;
            }
            int width = option.Size.Width;
            int height = option.Size.Height;
            var image = new Bitmap(width, height);

            using (Graphics gr = Graphics.FromImage(image))
            {
                gr.Clear(option.Title.BgColor);
                gr.TextRenderingHint = TextRenderingHint.AntiAlias;


                SizeF size = gr.MeasureString(title, option.Title.SimpleFont.ToFont());
                var x = (int)(width * 0.12);
                var y = (int)(height * 0.12);
                var displayRectangle = new RectangleF(new PointF(x, y), new SizeF(width - x * 2, (int)(height * 5.0 / 8 - 2 * y)));
                StringFormat format1 = new StringFormat(StringFormatFlags.FitBlackBox)
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center
                };
                using (var brush = new SolidBrush(option.Title.ForeColor))
                {
                    gr.DrawString(title, option.Title.SimpleFont.ToFont(), brush, (RectangleF)displayRectangle, format1);
                }

                using (var brush = new SolidBrush(option.Author.BgColor))
                {
                    int y2 = (int)(height * 5.0 / 8);
                    gr.FillRectangle(brush, new Rectangle(0, y2, width, height - y2));
                }

                using (var brush = new SolidBrush(option.Author.ForeColor))
                {

                    size = gr.MeasureString(author, option.Author.SimpleFont.ToFont());
                    int x3 = (int)(width * 0.9);
                    int y3 = (int)(height * 0.9);
                    displayRectangle = new RectangleF(new PointF(x3 - size.Width, y3 - size.Height), size);
                    gr.DrawString(author, option.Author.SimpleFont.ToFont(), brush, displayRectangle, format1);
                }
            }
            return image;
        }

        public static Image Generate(string title, string author)
        {
            return Generate(title, author, AppPreference.Load().CoverOption);
        }
    }
}
